﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D
{
    class Camera
    {
        public Matrix world;
        public Matrix view;
        public Matrix projection;
        public BoundingBox cameraCollision;
        KeyboardState key, lastKey;
        bool colidi;
        public float rotation;
        MouseState mouse;
        public float sensivity = 1;
        Vector2 lastMousePosition, mousePosition;
        public Boolean direita;
        float speed = 0.3f;
        public Vector3 position = new Vector3(250, 150, -100);

        public Camera()
        {
            world = Matrix.Identity;
            view = Matrix.CreateLookAt(position, Vector3.Zero, Vector3.Up);
            view *= Matrix.CreateTranslation(new Vector3(250, 100, -100));
            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Game1.TamanhoTela.X / (float)Game1.TamanhoTela.Y, 1, 500);
        }
        public void LookAt(Model3D obj)
        {
            view = Matrix.CreateLookAt(position, obj.position, Vector3.Up);
        }
        public void Update(List<GameObject3D> list)
        {
            mouse = Mouse.GetState();
            view = Matrix.Identity;

            mousePosition = new Vector2((float)mouse.X / 800, (float)mouse.Y / 600);

            key = Keyboard.GetState();

            colidi = false;
            foreach (GameObject3D ob in list)
            {
                if (ob.bBox.Intersects(this.cameraCollision))
                {
                    colidi = true;
                    Console.WriteLine("colidi");
                    //break;
                }
            }
            if ((mousePosition.X != lastMousePosition.X && mousePosition.X > lastMousePosition.X))
            {
                rotation -= 0.02f;
                Mouse.SetPosition(0, mouse.Y);
                mouse = Mouse.GetState();
                mousePosition = RmousePosition(mouse);
                direita = true;
            }
            else if ((mousePosition.X != lastMousePosition.X && mousePosition.X <= lastMousePosition.X))
            {
                rotation += 0.02f;
                Mouse.SetPosition(0, mouse.Y);
                mouse = Mouse.GetState();
                mousePosition = RmousePosition(mouse);
                direita = false;
            }

            if (key.IsKeyDown(Keys.Down))
            {
                if (colidi && lastKey != key)
                {
                    position.X += (float)(Math.Sin(rotation)) * speed;
                    position.Z += (float)(Math.Cos(rotation)) * speed;
                    lastKey = key;
                }
                if (colidi == false)
                {
                    position.X += (float)(Math.Sin(rotation)) * speed;
                    position.Z += (float)(Math.Cos(rotation)) * speed;
                    lastKey = key;
                }
            }
            if (key.IsKeyDown(Keys.Up))
            {
                if (colidi && lastKey != key)
                {
                    position.X -= (float)(Math.Sin(rotation)) * speed;
                    position.Z -= (float)(Math.Cos(rotation)) * speed;
                    lastKey = key;
                }

                if (colidi == false)
                {
                    position.X -= (float)(Math.Sin(rotation)) * speed;
                    position.Z -= (float)(Math.Cos(rotation)) * speed;
                    lastKey = key;
                }
            }
            if (key.IsKeyDown(Keys.J))
            {
                position = new Vector3(position.X, position.Y + 0.2f, position.Z);
            }
            if (key.IsKeyDown(Keys.K))
            {
                position = new Vector3(position.X, position.Y - 0.2f, position.Z);
            }

            lastMousePosition = mousePosition;

            view = Matrix.Identity;

            Console.WriteLine("rotacao" + MathHelper.ToDegrees(rotation)%360);
            Console.WriteLine("posicao" + position);
            view *= Matrix.CreateRotationY(rotation);
            view *= Matrix.CreateTranslation(position);
            view = Matrix.Invert(view);
            cameraCollision = new BoundingBox(position, new Vector3(position.X + 1, position.Y + 1, position.Z + 1));
        }
        public Vector2 RmousePosition(MouseState mouse)
        {
            return new Vector2((float)mouse.X / 800, (float)mouse.Y / 600);
        }
    }
}
